class Post < ActiveRecord::Base
  include TrashModel

  validates :title, presence: true
  validates :slug, presence: true, uniqueness: true, if: -> { published_at.present? }

  has_one :post_category, dependent: :destroy
  has_one :category, through: :post_category

  scope :published, -> { where.not(published_at: nil) }

  accepts_nested_attributes_for :post_category

  def published_time
    published_at && published_at.strftime('%Y-%m-%d')
  end

  def deleted_time
    deleted_at && deleted_at.strftime('%Y-%m-%d %H:%M')
  end
end
